Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
Is there an Angular way of doing: document.getElementById(id).style.display = "none";

I have a div with the id of 1. I'm trying to set the display to none dynamically. Is there an Angular way of doing this. Currently, I'm using vanilla javascript. I was asking about doing this dynamically because there will be over 60 divs that will be created from an array.

In my html

<div *ngFor="let item of items; i = index;">
    <div id={{i}} (click)=hideDiv()></div> 
</div>

In my method

hideDiv() {
return document.getElementById('1').style.display = "none";
}

That works but I'm looking for the Angular way of doing the above.

It was suggested that I use @ViewChild. Here's what I've changed. I can't use a Template Reference Variable as the html divs are created dynamically. Unless someone can let me know how to create the template variables dynamically. Although I don't think it's possible to create template variables with a loop.

@ViewChild('imgId', { static: true }) elementRef: ElementRef<HTMLDivElement>;
imgId: string;

Then in the method I have:

this.imgId = event.path[0].attributes[1].value;
 this.elementRef.nativeElement.style.display = "none";

The event.path[0].attributes[1].value gets me the id of the image. The imgId shows when I console log it. It's still not changing the display on the div to none. Also I'm getting the error: Cannot read properties of undefined (reading 'nativeElement')

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

Yes, you can use the ViewChild query in Angular to do this. In your component, define a query like this:

@ViewChild('#1') elementRef: ElementRef<HTMLDivElement>;

Implement the AfterViewInit interface in your component, and inside it, use this:

this.elementRef.nativeElement.style.display = "none";
about 4 years ago · Santiago Gelvez Relatório

0

You can simply use ngIf for this

Component

  shouldDisplay: boolean = true;

  hide(): void {
    this.shouldDisplay = false;
  }
  show(): void {
    this.shouldDisplay = true;
  }

Html

<button (click)="hide()">Hide</button>
<button (click)="show()">Show</button>
<div *ngIf="shouldDisplay">this is the content</div>

Here is the working example

about 4 years ago · Santiago Gelvez Relatório

0

This is the Angular way: template

<div *ngIf="showMe"></div>

or

<div [hidden]="!showMe"></div>

TypeScript:

showMe: boolean;

hideDiv() {
    this.showMe = false;
}
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda